{% extends "templates/web.html" %}

{% block head_include %}
<link rel="stylesheet" href="/assets/frappe/css/fonts/fontawesome/font-awesome.min.css">
{% endblock %}

{% block page_content %}

<h1>{{ _("Attribution") }}</h1>
<p>
    {{ _("This software is built on top of many open source packages. We would like to thank the authors of these
    packages for their contribution.") }}
</p>

{% for app_info in apps %}
<section id="{{ app_info.name }}">
    <h2><a href="#{{ app_info.name }}">{{ app_info.name }}</a></h2>
    <table class="table">
        <tr>
            <th>{{ _("Authors") }}</th>
            <td>{{ app_info.authors }}</td>
        </tr>
        <tr>
            <th>{{ _("Description") }}</th>
            <td>{{ app_info.description }}</td>
        </tr>
        {% if app_info["dependencies"] %}
        <tr>
            <th>{{ _("Dependencies") }}</th>
            <td>
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>{{ _("Package") }}</th>
                            <th>{{ _("Type") }}</th>
                            <th>{{ _("License") }}</th>
                            <th>{{ _("Authors / Maintainers") }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for package in app_info["dependencies"] %}
                        <tr>
                            <td class="package">
                                {{ package.name | e }}
                            </td>
                            <td class="type">{{ package.type }}</td>
                            <td class="license"><i class="fa fa-spinner fa-spin"></i></td>
                            <td class="author"><i class="fa fa-spinner fa-spin"></i></td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </td>
        </tr>
        {% endif %}
    </table>
</section>
{% endfor %}

<script>
    const tables = document.querySelectorAll('table.table-striped');
    tables.forEach((table) => {
        var package_cells = table.querySelectorAll('td.package');
        package_cells.forEach((package_cell) => {
            var name = package_cell.innerText;
            var type_cell = package_cell.nextElementSibling;
            var license_cell = type_cell.nextElementSibling;
            var author_cell = license_cell.nextElementSibling;
            if (type_cell.innerText === "JavaScript") {
                get_info_from_npm(name).then((info) => {
                    license_cell.innerText = info.license?.slice(0, 50);
                    author_cell.innerText = info.author;
                });
            }
            else if (type_cell.innerText === "Python") {
                get_info_from_pypi(name).then((info) => {
                    license_cell.innerText = info.license?.slice(0, 50);
                    author_cell.innerText = info.author;
                });
            }
        });
    })

    function get_info_from_npm(package) {
        const registry_url = `https://registry.npmjs.org/${package}`;
        return fetch(registry_url)
            .then((response) => response.json())
            .then((data) => {
                return {
                    license: (
                        typeof data.license === "object" ? data.license.type : data.license
                    ) || "Unknown",
                    author: (
                        data.author?.name ||
                        data.maintainers?.map((c) => c.name).join(", ") ||
                        "Unknown"
                    )
                }
            });
    }

    function get_info_from_pypi(package) {
        const registry_url = `https://pypi.org/pypi/${package}/json`;
        return fetch(registry_url)
            .then((response) => response.json())
            .then((data) => {
                return {
                    license: data.info.license || "Unknown",
                    author: (
                        data.info.author ||
                        data.info.maintainer ||
                        data.info.author_email ||
                        data.info.maintainer_email ||
                        "Unknown"
                    )
                }
            });
    }
</script>

{% endblock %}